#!/usr/bin/env python
# coding=utf-8
# Author:  vavava
# Created: 04/11/2012

if __name__ == '__main__':
    """ for file debug"""
    import sys,os
    sys.path.insert(0,os.path.join( os.getcwd(), '..' ))


from vavava.basethread import BaseThread
import MySQLdb,time
from DBUtils import PooledDB


class HHHH(BaseThread):
    def __init__(self,dbpool,name="dbtest",log=None):
        BaseThread.__init__( self,name,log)
        self.dbpool=dbpool

    def run(self):
        self.conn = self.dbpool.connection()
        while self.IsRunning:
            cursor=self.conn.cursor()
            result = cursor.execute("select * from wr_trace.tracking")
            res = cursor.fetchone()
            self.log.info(res)
            time.sleep(0.5)
            cursor.close()
    def Stop(self,timeout):
        if self.conn:
            self.conn.close()
        self.running_stop()
        self.join(timeout)



def main():

    import vavava.util
    log = vavava.util.initlog("./log/test.log")

    try:
        threads=[]
        dbpool = PooledDB.PooledDB(
            MySQLdb,100,50,100,400,False,
            host='localhost',user='root',passwd='root',db='test',
            charset='utf8' )

        for i in range(10):
            tt = HHHH(dbpool,log=log)
            tt.running_start()
            threads.append(tt)

        while True:
            time.sleep(0.5)
    except(KeyboardInterrupt):
        print('main thread(%f):stop'%time.clock())

        for thread in threads:
            thread.Stop(10)
        print('main thread(%f):stopped'%time.clock())

if __name__ == '__main__':
    main()


